<template>
  <ly-dialog
    v-model="visible"
    :title="$t('route.cilpMoney') + '--' +'修改主信息'"
    width="500px"
    top="10vh"
    :confirm="handleConfirm"
    :cancel="handleCancel"
    :before-close="() => {$emit('update:visible', false)}"
    @open="handleOpen"
  >
    <ly-form
      ref="form"
      size="mini"
      label-width="80px"
      :model="temp"
    >
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="到账日期"
            prop="orderDate"
          >
            <el-date-picker
              v-model="temp.orderDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="进展"
            prop="progress"
          >
            <el-select
              v-model="temp.progress"
              clearable
              placeholder="请选择进展"
              @change="handleChange($event,'progress')"
            >
              <el-option
                v-for="item in progressList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="类型"
            prop="type"
          >
            <el-select
              v-model="temp.type"
              clearable
              placeholder="请选择类型"
              @change="handleChange($event,'type')"
            >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="结算方式"
            prop="settlement"
          >
            <el-select
              v-model="temp.settlement"
              clearable
              placeholder="请选择结算方式"
              @change="handleChange($event,'settlement')"
            >
              <el-option
                v-for="item in settlementList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="提现金额"
            prop="amount"
          >
            <el-input v-model="temp.amount" type="number" placeholder="请填写" @input="handleAmount($event,'amount')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="手续费"
            prop="handlingFees"
          >
            <el-input v-model="temp.handlingFees" type="number" placeholder="请填写" @input="handleAmount($event,'handlingFees')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="实际到账"
            prop="receipt"
          >
            <el-input v-model="temp.receipt" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="修改日期"
            prop="modifyTime"
          >
            <el-date-picker
              v-model="temp.modifyTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </ly-form>
  </ly-dialog>
</template>

<script>
import NP from 'number-precision'
import { updateOne } from '@/api/received'

export default {
  name: 'ModifyMasterDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: undefined
    },
    multiple: {
      type: Array,
      default: undefined
    },
    progressList: {
      type: Array,
      default: undefined
    },
    typeList: {
      type: Array,
      default: undefined
    },
    settlementList: {
      type: Array,
      default: undefined
    }
  },
  data() {
    return {
      temp: {
        orderDate: '',
        progress: '',
        progressName: '',
        type: '',
        typeName: '',
        settlement: '',
        settlementName: '',
        amount: '',
        receipt: '',
        handlingFees: '',
        modifyTime: '',
        cartonOrderNOs: []
      },
      restTemp: {
        orderDate: '',
        progress: '',
        progressName: '',
        type: '',
        typeName: '',
        settlement: '',
        settlementName: '',
        amount: '',
        receipt: '',
        handlingFees: '',
        modifyTime: '',
        cartonOrderNOs: []
      }
    }
  },
  methods: {
    async handleConfirm() {
      this.temp.cartonOrderNOs = this.multiple
      const list = this.temp.cartonOrderNOs.filter(item => { return item.checked === 1 })
      if (list.length > 0) {
        this.$confirm('检测到有审核的数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          const result = await updateOne('getCilpMoneyBatchUpdate', this.temp)
          if (result.code === 200) {
            this.setVisible(false)
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.$emit('add-data')
          }
        }).catch(() => {
        })
      } else {
        const result = await updateOne('getCilpMoneyBatchUpdate', this.temp)
        if (result.code === 200) {
          this.setVisible(false)
          this.$notify({
            title: '成功',
            message: '修改成功',
            type: 'success',
            duration: 2000
          })
          this.$emit('add-data')
        }
      }
    },
    // 类型
    handleChange(id, val) {
      if (val === 'progress') {
        const list = this.progressList.filter(item => item.id === id)
        this.temp.progressName = list[0].name
      } else if (val === 'type') {
        const list = this.typeList.filter(item => item.id === id)
        this.temp.typeName = list[0].name
      } else {
        const list = this.settlementList.filter(item => item.id === id)
        this.temp.settlementName = list[0].name
      }
    },
    handleAmount(val, id) {
      if (id === 'amount' && val > 0) {
        this.temp.receipt = NP.round(NP.minus(Number(val), this.temp.handlingFees), 2)
      } else if (id === 'handlingFees' && val > 0) {
        this.temp.receipt = NP.round(NP.minus(Number(this.temp.amount), Number(val)), 2)
      }
    },
    handleOpen() {
      this.temp = Object.assign({}, this.restTemp)
      if (this.multiple.length === 1) {
        const {
          orderDate,
          progress,
          progressName,
          type,
          typeName,
          settlement,
          settlementName,
          amount,
          receipt,
          handlingFees,
          modifyTime
        } = this.multiple[0]

        this.temp = {
          orderDate,
          progress,
          progressName,
          type,
          typeName,
          settlement,
          settlementName,
          amount,
          receipt,
          handlingFees,
          modifyTime,
          cartonOrderNOs: []
        }
      }
    },
    handleCancel() {
      this.setVisible(false)
    },
    setVisible(flag) {
      this.$emit('update:visible', flag)
    }
  }
}
</script>

<style>

</style>
